package com.future.message.Controller;

import com.future.message.Service.FileService;
import com.future.message.Service.IndexService;
import com.future.message.Utils.ResponseResult;
import com.future.message.pojo.Index;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "指标")
@RequestMapping("/index")
public class IndexController {
    @Resource
    IndexService indexService;
    @Resource
    FileService fileService;

    @ApiOperation(value = "添加一级指标")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexName", value = "指标内容", required = true)
            }
    )
    @PostMapping("/addOneIndex")
    @Secured("ROLE_1")
    public ResponseResult addOneIndex(String indexName) {
        return indexService.addOneIndex(indexName);
    }


    @ApiOperation(value = "添加二级指标")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexName", value = "指标内容", required = true),
                    @ApiImplicitParam(name = "indexFatherId", value = "父指标id", required = true)
            }
    )
    @PostMapping("/addTwoIndex")
    @Secured("ROLE_1")
    public ResponseResult addTwoIndex(String indexName,Integer indexFatherId) {
        return indexService.addTwoIndex(indexName,indexFatherId);
    }


    @ApiOperation(value = "添加三级指标")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexName", value = "指标内容", required = true),
                    @ApiImplicitParam(name = "indexFatherId", value = "父指标id", required = true)
            }
    )
    @PostMapping("/addThreeIndex")
    @Secured("ROLE_1")
    public ResponseResult addThreeIndex(String indexName,Integer indexFatherId) {
        return indexService.addThreeIndex( indexName, indexFatherId);
    }


    @ApiOperation(value = "删除指标")
    @DeleteMapping("/deleteIndexById")
    @Secured("ROLE_1")
    public ResponseResult deleteIndexById(Integer indexId) {
        return indexService.deleteIndexById(indexId);
    }


    @ApiOperation(value = "批量删除指标")
    @DeleteMapping("/deleteIndexList")
    @Secured("ROLE_1")
    public ResponseResult deleteIndexList(Integer []indexId,Integer indexNowGrade) {
        return indexService.deleteIndexList(indexId,indexNowGrade);
    }

    @ApiOperation(value = "修改指标")
    @PutMapping("/updateIndex")
    @Secured("ROLE_1")
    public ResponseResult updateIndex(String content,Integer indexId) {
        return indexService.updateIndex(content, indexId);
    }

    @ApiOperation(value = "通过id查找")
    @GetMapping("/findIndexById")
    public ResponseResult findIndexById(Integer indexId) {
        return indexService.findIndexById(indexId);
    }

    @ApiOperation(value = "通过指标等级和父指标id遍历指标--后台")
    @GetMapping("/findAllIndexByIndexNowGrade")
    @Secured("ROLE_1")
    public ResponseResult findAllIndexByIndexNowGrade(Integer indexNowGrade, Integer indexFatherId) {
        return indexService.findAllIndexByIndexNowGrade(indexNowGrade, indexFatherId);
    }

    @ApiOperation(value = "通过指标等级和父指标id和年份遍历指标--后台")
    @GetMapping("/findAllIndexByIndexNowGradeAndYear")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexNowGrade", value = "当前指标等级",required = true),
                    @ApiImplicitParam(name = "indexFatherId", value = "父指标id",required = true),
                    @ApiImplicitParam(name = "year", value = "年份(2021,2022)等",required = true)
            }
    )
    @Secured("ROLE_1")
    public ResponseResult findAllIndexByIndexNowGradeAndYear(Integer indexNowGrade, Integer indexFatherId,String year) {
        return indexService.findAllIndexByIndexNowGradeAndYear(indexNowGrade, indexFatherId,year);
    }

//    @ApiOperation(value = "根据指标等级,父指标id,用户id,遍历自己的指标")
//    @GetMapping("/findIndexByUserIdAndIndexNowGrade")
//    @Secured("ROLE_1")
//    public ResponseResult findIndexByUserIdAndIndexNowGrade(Integer userId,Integer indexNowGrade, Integer indexFatherId) {
//        return indexService.findIndexByUserIdAndIndexNowGrade(userId,indexNowGrade, indexFatherId);
//    }

    @ApiOperation(value = "上传三级指标的模板文件")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexId", value = "三级指标id",required = true)
            }
    )
    @PutMapping("/updateData")
    public ResponseResult updateSrc(Integer indexId, MultipartFile[] data) {
        return indexService.updateData(indexId, data);
    }

    @ApiOperation(value = "下载模板文件")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexId", value = "三级指标id",required = true),
                    @ApiImplicitParam(name = "fileName", value = "文件名字",required = true),
            }
    )
    @GetMapping("/downLoadIndexFile")
    public void downLoadIndexFile( Integer indexId, String fileName, HttpServletResponse response){
        fileService.downLoadIndexFile(indexId, fileName, response);
    }

    @ApiOperation(value = "一键打包")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexId", value = "三级指标id",required = true)
            }
    )
    @GetMapping("/fileToZip")
    public void fileToZip( Integer indexId,HttpServletResponse response){
        fileService.fileToZip(indexId,response);
    }

    @ApiOperation(value = "删除模板文件")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "indexId", value = "指标id",required = true),
            }
    )
    @DeleteMapping("/deleteIndexFile")
    public ResponseResult deleteIndexFile(String name,Integer indexId){
        return fileService.deleteIndexFile(name,indexId);
    }


    @ApiOperation(value = "一键继承某一年的指标")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "year", value = "年份", required = true)
            }
    )
    @PostMapping("/extendsIndex")
    @Secured("ROLE_1")
    public ResponseResult extendsIndex(String year) {
        return indexService.extendsIndex(year);
    }

}
